package com.cloudsigns.factorymanager2.produce.activity.produce_team;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IApplyAdjustFeedbackView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.ApplyAdjustFeedbackPresenter;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import de.greenrobot.event.EventBus;

public class ProduceTeamDailyFeedbackApplyAdjustmentActivity extends BaseActivity implements IApplyAdjustFeedbackView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private EditText edt_adjust_day_num, edt_adjust_reason;
    private ImageView img_adjust_day_num, img_adjust_reason;
    private Button btn_submit;

    private WorkPartBean workPartBean;
    private WorkPartOrderBean workPartOrderBean;
    private ApplyAdjustFeedbackPresenter applyAdjustFeedbackPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_team_daily_feedback_apply_adjustment);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_adjust_day_num = findViewById(R.id.edt_adjust_day_num);
        edt_adjust_reason = findViewById(R.id.edt_adjust_reason);
        img_adjust_day_num = findViewById(R.id.img_adjust_day_num);
        img_adjust_reason = findViewById(R.id.img_adjust_reason);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.application_adjustment));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            workPartOrderBean = (WorkPartOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_2);
        }

        applyAdjustFeedbackPresenter = new ApplyAdjustFeedbackPresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_adjust_day_num.setOnClickListener(this);
        img_adjust_reason.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 调整天数
            case R.id.img_adjust_day_num:
                break;
            // 调整原因
            case R.id.img_adjust_reason:
                break;
            // 提交
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    private void submit() {
        if (TextUtils.isEmpty(edt_adjust_day_num.getText().toString())) {
            showToast(getResources().getString(R.string.toast_days));
        } else if (0 > Integer.parseInt(edt_adjust_day_num.getText().toString().trim())) {
            showToast(getResources().getString(R.string.toast_days_greater_zero));
        } else if (TextUtils.isEmpty(edt_adjust_reason.getText().toString())) {
            showToast(getResources().getString(R.string.toast_reason));
        } else {
            applyAdjustFeedbackPresenter.applyAdjustFeedback();
        }
    }

    @Override
    public String getContent() {
        return edt_adjust_reason.getText().toString();
    }

    @Override
    public int getDay() {
        return Integer.parseInt(edt_adjust_day_num.getText().toString());
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderBean.getWorkPartOrderId();
    }

    @Override
    public void applyAdjustFeedbackSuccessed() {
        showToast("提交成功");
        EventBus.getDefault().post(new ProduceForTeamEvent());
        finish();
    }

    @Override
    public void applyAdjustFeedbackFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
